// pages/seach/seach.js
const app = getApp()
Page({
  data: {
    title: '搜索',
    seachrecord:[],
    classification: [],
    seachkey: '',
    isIphoneX: false,
    statusBarHeight: app.globalData.statusBarHeight
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '京多多',
      path: '/pages/index/index?userId=' + app.globalData.userId,
      imageUrl: '/img/sharepic.jpg'
    }
  },
  //事件处理函数
  onLoad: function () {
    let isIphoneX = app.globalData.isIphoneX;
    this.setData({
      isIphoneX: isIphoneX
    })
    this.getseachrecordData();
    this.getclassificationdata();
  },
  getseachrecordData() {
    var that = this
    wx.getStorage({
      key: 'key',
      success: function (res) {
        console.log(res.data)
        app.globalData.seachrecord = res.data
        that.setData({
          seachrecord: res.data,
        });
      }
    })
  },
  getclassificationdata() {
    var that = this
    wx.getStorage({
      key: 'classification',
      success: function (res) {
        console.log(res.data)
        that.setData({
          classification: res.data,
        });
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
    const requestTask = wx.request({
      method: "POST",
      header: { "content-type": "application/x-www-form-urlencoded" },
      data: {
        
      },
      url: app.globalData.url + "/app/v2/goods/categorys",
      async: true,
      timeout: 10000,
      success: function (data) {
        console.log(data)
        that.setData({
          classification: data.data,
        });
        console.log(that.data.classification)
      },
      error: function (textStatus) {
        console.error(textStatus);
      },
      complete: function (XMLHttpRequest, status) {
        if (status == 'timeout') {
          requestTask.abort()    // 超时后中断请求
          wx.showModal({
            title: '提示',
            content: '网络超时，请重试',
            showCancel: 'false',
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/pages/index/index'
                })
              }
            }
          })
        }
      }
    })
  },
  formSubmit: function (e) {
    var that = this
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var keywordcooke = app.globalData.seachrecord,
        keyword = e.detail.value,
        repeat = false;
    console.log('keywordcooke' + keywordcooke)
    for (var i = 0; i < keywordcooke.length; i++) {
      console.log(keywordcooke[i])
      if (keywordcooke[i] == keyword) {
        keywordcooke.splice(i, 1);
        keywordcooke.unshift(keyword)
        repeat = true;
        break;
      }
    }
    if (!repeat) {
      keywordcooke.unshift(keyword)
    }
    console.log('keywordcooke' + keywordcooke)
    wx.setStorage({
      key: "key",
      data: keywordcooke
    })
    app.globalData.seachkey = e.detail.value
    wx.getStorage({
      key: 'key',
      success: function (res) {
        console.log(res.data)
        that.setData({
          seachrecord: res.data,
        });
      }
    })
    console.log(app.globalData.seachkey)
    wx.navigateTo({
      url: '/pages/seachlist/seachlist'
    })
  },
  forminput: function(e){
    console.log(e)
    this.setData({
      seachkey: e.detail.value,
    });
  },
  back: function () {
    console.log('back')
    wx.redirectTo({
      url: '/pages/itemlist/itemlist'
    })
  },
  cancel: function () {
    wx.redirectTo({
      url: '/pages/itemlist/itemlist'
    })
  },
  delrecord: function () {
    wx.clearStorage()
    app.globalData.seachrecord = []
    this.setData({
      seachrecord: [],
    });
  },
  reset: function () {
    this.setData({
      seachkey: '',
    });
  },
  seachrecord: function (e) {
    console.log(e.currentTarget.dataset.keyid[e.currentTarget.dataset.index])
    app.globalData.seachkey = e.currentTarget.dataset.keyid[e.currentTarget.dataset.index]
    app.globalData.seachid = ''
    console.log(app.globalData.seachkey)
    wx.navigateTo({
      url: '/pages/seachlist/seachlist'
    })
  },
  seachclassification: function (e) {
    app.globalData.seachid = e.currentTarget.dataset.key
    app.globalData.seachkey = ''
    console.log(app.globalData.seachid)
    wx.navigateTo({
      url: '/pages/seachlist/seachlist'
    })
  },
})
